//
//  LTFReadContentView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/20.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFReadContentView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import <WebKit/WebKit.h>
#import "LTFReadContentModel.h"
#import "IsNight.h"
@interface LTFReadContentView ()
<
 UIWebViewDelegate
>
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) LTFReadContentModel *modelV;
@end

@implementation LTFReadContentView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}


- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
        [self createData];
    }
}
- (void)createView {
    self.webView = [UIWebView new];
    [self addSubview:self.webView];
    
    self.webView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    self.webView.delegate = self;
    
}

- (void)createData {
    NSString *htmlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.string];
    NSString *string = [htmlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *dic = [dictionary objectForKey:self.string];
        self.modelV = [[LTFReadContentModel alloc] init];
        [self.modelV setValuesForKeysWithDictionary:dic];        
        [self loadMyWebView];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];

    
}

- (void)loadMyWebView
{
    [self loadIMG];
    
    self.modelV.body = [NSString stringWithFormat:@"<h2>%@</h2>%@  %@\n%@\n", self.modelV.title, self.modelV.source, self.modelV.ptime, self.modelV.body];
    [self.webView loadHTMLString:self.modelV.body baseURL:nil];
}

- (void)loadIMG
{
    for (NSInteger i = 0; i < self.modelV.img.count; i++) {
        
        NSString *length = [[self.modelV.img objectAtIndex:i] objectForKey:@"pixel"];
        NSDictionary *dic = [self.modelV.img objectAtIndex:i];
        
        
        NSArray *arr = [length componentsSeparatedByString:@"*"];
        NSString *imgStr = [NSString stringWithFormat:@"<!--IMG#%ld-->", i];
        // 拼接
        CGFloat height = [arr[0] integerValue];
        CGFloat width = [arr[1] integerValue];
        width = self.frame.size.width - 20;
        height = width * 0.6;
        
        NSString *str = [NSString stringWithFormat:@"<img src='%@'  height='%f' width='%f' />\n",[dic objectForKey:@"src"], height, width];
        
        // 拼接内容
        self.modelV.body = [self.modelV.body stringByReplacingOccurrencesOfString:imgStr withString:str];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
